<?php
/*
+-----------------------------------------------------------------------------+
| $Id: *.php 2009-08-18 08:41:22Z Bleakwind $
| Copyright (c) 2003-2010 Bleakwind (www.weaverdream.com)
| http://www.weaverdream.com/
+-----------------------------------------------------------------------------+
*/

if (!defined( 'ENTRY_INDEX')){
    echo "<h1>Forbidden</h1><p>You don't have permission to access on this server.</p>";
    exit;
}

if($sys->get['ope'] == "save"){
    // check the post
    if (!empty($sys->post['del'])){
        $where = "id IN (".implode(",", $sys->post['del']).")";
    }else{
        $where = "id IN (0)";
    }

    // delete
    $sql = "DELETE FROM ".DB_TABLE_PM_OUT."
                WHERE ".$where."";
    $result = $db->Execute($sql);            
    if (!$result) {
        echo $db->ErrorMsg();
    }

    $sys->cp_prompt("jump",$CONFIGURE['common']['control_index']."?act=cp_pm_out");

}elseif($sys->get['ope'] == "del"){

    if(!preg_match("/^[0-9]+$/i",$sys->get['piid'])){
        $sys->prompt("failed",$LANGUAGE['s']['cp_pm_out']['del_piid_error']."<!-- piid error -->");
    }else{
        // delete
        $sql = "DELETE FROM ".DB_TABLE_PM_OUT."
                    WHERE id=".$sys->get['piid'];
        $result = $db->Execute($sql);            
        if (!$result) {
            echo $db->ErrorMsg();
        }
    
        $sys->cp_prompt("jump",$CONFIGURE['common']['control_index']."?act=cp_pm_out&pag=".$sys->get['pag']);
    }

}elseif($sys->get['ope'] == "detail"){
    if(!preg_match("/^[0-9]+$/i",$sys->get['piid'])){
        $sys->prompt("failed",$LANGUAGE['s']['cp_pm_out']['detail_piid_error']."<!-- piid error -->");
    }else{
        $sql = "SELECT pi.*
                FROM ".DB_TABLE_PM_OUT." pi
                WHERE pi.id=".$sys->get['piid']."";
        $result = &$db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        } else {
            if (!$result->EOF) {

                $member_info = $sys->return_member($result->fields['tid'], array("id","username"));

                $cp_pm_out_list = array(
                       'id'         => $result->fields['id'],
                       'subject'    => $result->fields['subject'],
                       'content'    => $result->fields['content'],
                       'fid'        => $result->fields['fid'],
                       'tid'        => $result->fields['tid'],
                       'mid'        => $result->fields['mid'],
                       'if_read'    => $result->fields['if_read'],
                       'time'       => $result->fields['time'],
                       'm_id'       => $member_info['m_id'],
                       'm_username' => $member_info['m_username'],
        		);
            }
        }

        $sql = "UPDATE ".DB_TABLE_PM_OUT." SET
                    if_read =1
                WHERE id=".$sys->get['piid'];
        $result = $db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        }

        $t->assign(array(
            "cp_pm_out_list" => $cp_pm_out_list,
            "pagination"     => $bwpage->pagination,
        ));
    }
}else{
    
    $total_record = func::db_count_record(DB_TABLE_PM_OUT, "fid=".$MEMBER['id']."");
    $bwpage->set_record_listnum($CONFIG['page_record_cp_pm_out']);
    $bwpage->set_record($total_record);
    $bwpage->page($CONFIGURE['common']['control_index']."?act=cp_pm_out&");
    $sql = "SELECT pi.*
            FROM ".DB_TABLE_PM_OUT." pi
            WHERE pi.fid=".$MEMBER['id']."
            ORDER BY pi.time DESC, pi.id DESC
            LIMIT ".$bwpage->record_listfirst.",".$bwpage->record_listnum;
    $result = &$db->Execute($sql);
    if (!$result) {
        echo $db->ErrorMsg();
    } else {
        while (!$result->EOF) {

            $member_info = $sys->return_member($result->fields['tid'], array("id","username"));

            $cp_pm_out_list[] = array(
                   'id'         => $result->fields['id'],
                   'subject'    => $result->fields['subject'],
                   'content'    => $result->fields['content'],
                   'fid'        => $result->fields['fid'],
                   'tid'        => $result->fields['tid'],
                   'mid'        => $result->fields['mid'],
                   'if_read'    => $result->fields['if_read'],
                   'time'       => $result->fields['time'],
                   'm_id'       => $member_info['m_id'],
                   'm_username' => $member_info['m_username'],
    		);
    		$result->MoveNext();
        }
    }
    
    $t->assign(array(
        "cp_pm_out_list" => $cp_pm_out_list,
        "pagination"    => $bwpage->pagination,
    ));

}
$public_var['page_place'][] = $LANGUAGE['s']['cp_pm_out']['page_place_upper'];
$public_var['page_place'][] = $LANGUAGE['s']['cp_pm_out']['page_place'];
?>
